package series;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

public class EpisodeDAO {

	@Autowired
	private JdbcTemplate db;

	private static final class EpisodeRowMapper implements
			ParameterizedRowMapper<Episode> {

		@Override
		public Episode mapRow(ResultSet rs, int rowNum) throws SQLException {
			String format = String.format("%d, %d,%d,%d,%s", rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getInt(5), rs.getString(4));
			System.out.println("hmmmm" + format);
			Episode entry = new Episode(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getInt(5), rs.getString(4));
			return entry;
		}
	}

	public EpisodeDAO(JdbcTemplate db) {
		super();
		this.db = db;
	}
	
	public List<Episode> episodesForSeriesIdAndSeasonId(int seriesId, int seasonId) {
		String sql = "select * from episodes where series_id='" + seriesId + "' and season_id='" + seasonId + "'";
		System.out.println(sql);
		List<Episode> episodesList = db.query(sql, new EpisodeRowMapper());
		System.out.println(episodesList);
		return episodesList;
	}
}
